<?php
/**
 * @author   Evient
 * @email    xusc@outlook.com
 * @create   2017-06 16:17
 * @project  haodiaoju
 */

defined('IN_KG') or exit('Access Invalid!');

class storage_managerControl extends BaseSellerControl{

    public function indexOp(){
        $where['store_id'] = $_SESSION['store_id'];
        $where['goods_storage'] = array('lt',3);
        $where['goods_state'] = 1;
        $goods_list = Model('goods')->where($where)->select();

        $storage_warning_goods = Model()->query('select goods_id from shop_goods_storage');
        $warning_goodsid_list = array_column($storage_warning_goods,'goods_id');

        $storage_list = array();
        foreach ($goods_list as $item =>$value){
            if (in_array($value['goods_id'], $warning_goodsid_list)){
                continue;
            }
            $storage_list[] = array(
                'goods_id'  => $value['goods_id'],
                'warning_time' => time(),
                'storage_status' => 0,
                'warning_status' => 1,
            );
        }
        if (!empty($storage_list)){
            Model('goods_storage')->insertAll($storage_list);
        }
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $page_count = 50;
        $page_index = ($page - 1) * $page_count;

        $sql = <<<sql
                SELECT
                    gd.goods_id,
                    gd.goods_commonid,
                    gd.goods_name,
                    gd.goods_serial,
                    gd.goods_image,
                    gd.goods_spec,
                    gd.goods_storage,
                    se.warning_time,
                    se.storage_status,
                    se.warning_status
                FROM
                    shop_goods AS gd
                RIGHT JOIN shop_goods_storage AS se 
                ON gd.goods_id = se.goods_id
                where gd.goods_storage < 3
                and gd.goods_state = 1
                and se.warning_status = 1 order by gd.goods_storage limit $page_index,$page_count;
sql;

        $goods_new_list = Model()->query($sql);
        foreach ($goods_new_list as $item => $value){
            $spec = array_values(unserialize($value['goods_spec']));
            $goods_new_list[$item]['goods_spec'] = $spec[0].' '.$spec[1];
        }
        Tpl::output('goods_list',$goods_new_list);

        $page_list = new Page();
        $countSql = "select count(*) as count from shop_goods as gd LEFT join shop_goods_storage as st on gd.goods_id = st.goods_id where gd.goods_state = 1 and gd.store_id = 1 and gd.goods_storage < 3 and st.warning_status = 1";
        $storage_warring = Model()->query($countSql);
        $data_count = $storage_warring[0]['count'];
        $page_list->setTotalNum($data_count);
        $page_list->setNowPage($page);
        $page_list->setEachNum($page_count);
        Tpl::output('nav_list',$page_list->show());

        Tpl::showpage('storage_index');
    }

    /*更改商品状态*/
    public function change_statusOp(){
        $field = [
            'goods_id'  => '/\d+/',
            'status'    => '/\d/',
        ];

        foreach ($field as $key => $rule){
            if (!preg_match($rule, $_POST[$key])){
                echo json_encode(['code' => 200, 'msg' => $key.'数据错误']);
                die;
            }
        }

        $where['goods_id'] = $_POST['goods_id'];
        Model('goods_storage')->where($where)->update(['storage_status' => $_POST['status']]);
    }

    /*停止报警*/
    public function stop_warningOp(){
        if (!preg_match('/\d+/', $_POST['goods_id'])){
            echo json_encode(['code' => 400, 'msg' => '错误的商品ID']);
            die;
        }

        $where['goods_id'] = $_POST['goods_id'];
        Model('goods_storage')->where($where)->update(['warning_status' => 0]);
        echo json_encode(['code' => 200, 'msg' => '删除成功']);
    }
}